{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "niftynet_path = '/home/tom/phd/NiftyNet-Generator-PR/NiftyNet'\n",
    "sys.path.append(niftynet_path)\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "from niftynet.io.image_reader import ImageReader\n",
    "from niftynet.io.image_sets_partitioner import ImageSetsPartitioner\n",
    "from collections import namedtuple\n",
    "\n",
    "from niftynet.contrib.preprocessors.preprocessing import Preprocessing\n",
    "from niftynet.contrib.csv_reader.sampler_csv_rows import ImageWindowDatasetCSV\n",
    "from niftynet.contrib.csv_reader.sampler_resize_v2_csv import ResizeSamplerCSV as ResizeSampler\n",
    "from niftynet.contrib.csv_reader.csv_reader import CSVReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#### Some setup\n",
    "NetParam = namedtuple('NetParam', 'normalise_foreground_only foreground_type multimod_foreground_type histogram_ref_file norm_type cutoff normalisation whitening')\n",
    "ActionParam = namedtuple('ActionParam', 'random_flipping_axes scaling_percentage rotation_angle rotation_angle_x rotation_angle_y rotation_angle_z do_elastic_deformation num_ctrl_points deformation_sigma proportion_to_deform')\n",
    "class TaskParam:\n",
    "    def __init__(self, classes):\n",
    "        self.image = classes\n",
    "net_param = NetParam(normalise_foreground_only=False, foreground_type='threshold_plus', multimod_foreground_type = 'and', histogram_ref_file='mapping.txt', norm_type='percentile', cutoff=(0.05, 0.95), normalisation=False, whitening=True)\n",
    "action_param = ActionParam(random_flipping_axes=[], scaling_percentage=[], rotation_angle=None, rotation_angle_x=None, rotation_angle_y=None, rotation_angle_z=None, do_elastic_deformation=False, num_ctrl_points=6, deformation_sigma=50, proportion_to_deform=0.9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1) Create a csv of labels and show how it can be returned by the CSV Reader\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accessing: https://github.com/NifTK/NiftyNetModelZoo\n",
      "mr_ct_regression_model_zoo_data: FAIL. \n",
      "No NiftyNet example was found for mr_ct_regression_model_zoo_data.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>subject_id</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>RAN</td>\n",
       "      <td>RAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HAL</td>\n",
       "      <td>HAL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MIL</td>\n",
       "      <td>MIL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHA</td>\n",
       "      <td>CHA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>GRA</td>\n",
       "      <td>GRA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>PLA</td>\n",
       "      <td>PLA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NAR</td>\n",
       "      <td>NAR</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>WEB</td>\n",
       "      <td>WEB</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>PAR</td>\n",
       "      <td>PAR</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>HON</td>\n",
       "      <td>HON</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>HAF</td>\n",
       "      <td>HAF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LEW</td>\n",
       "      <td>LEW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>SOU</td>\n",
       "      <td>SOU</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>SPE</td>\n",
       "      <td>SPE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>CRI</td>\n",
       "      <td>CRI</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   subject_id label\n",
       "0         RAN   RAN\n",
       "1         HAL   HAL\n",
       "2         MIL   MIL\n",
       "3         CHA   CHA\n",
       "4         GRA   GRA\n",
       "5         PLA   PLA\n",
       "6         NAR   NAR\n",
       "7         WEB   WEB\n",
       "8         PAR   PAR\n",
       "9         HON   HON\n",
       "10        HAF   HAF\n",
       "11        LEW   LEW\n",
       "12        SOU   SOU\n",
       "13        SPE   SPE\n",
       "14        CRI   CRI"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from niftynet.utilities.download import download\n",
    "download('mr_ct_regression_model_zoo_data')\n",
    "labels_location = 'ct.csv'\n",
    "files = [file for file in os.listdir('/home/tom/niftynet/data/mr_ct_regression/CT_zero_mean') if file.endswith('.nii.gz')]\n",
    "pd.DataFrame(data=[(file.replace('.nii.gz', ''), file.replace('.nii.gz', '')) for file in files]).to_csv('label.csv', index=None, header=['subject_id', 'label'])\n",
    "pd.read_csv('label.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1mINFO:niftynet:\u001b[0m \n",
      "\n",
      "Number of subjects 15, input section names: ['subject_id', 'CT']\n",
      "-- using all subjects (without data partitioning).\n",
      "\n",
      "\u001b[1mINFO:niftynet:\u001b[0m Image reader: loading 15 subjects from sections ['CT'] as input [image]\n",
      "\u001b[1mWARNING:niftynet:\u001b[0m This method will read your entire csv into memory\n",
      "One sample from the csv_reader: [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
      "\u001b[1mINFO:niftynet:\u001b[0m reading size of preprocessed images\n",
      "\u001b[1mWARNING:niftynet:\u001b[0m sampler queue_length should be larger than batch_size, defaulting to batch_size * 5.0 (10).\n",
      "(1, 100, 100, 1, 1, 1)\n",
      "(1, 15, 1, 1, 1, 1)\n"
     ]
    }
   ],
   "source": [
    "#### Testing the CSV Reader on labels\n",
    "# Make sure we accept 'Label', 'label', 'LABEL'\n",
    "task_param = TaskParam(['image'])\n",
    "image_data_param = {'CT': {'path_to_search': '~/niftynet/data/mr_ct_regression/CT_zero_mean', 'filename_contains': 'nii'}}\n",
    "#csv_data_file is a csv with data\n",
    "csv_data_param = {'label': {'csv_data_file': 'label.csv', 'to_ohe': True}}\n",
    "grouping_param = {'image': (['CT'])}\n",
    "\n",
    "image_sets_partitioner = ImageSetsPartitioner().initialise(image_data_param)\n",
    "image_reader = ImageReader().initialise(image_data_param, grouping_param, file_list=image_sets_partitioner.all_files)\n",
    "preprocessing = Preprocessing(net_param, action_param, task_param)\n",
    "normalisation_layers = preprocessing.prepare_normalisation_layers()\n",
    "augmentation_layers = preprocessing.prepare_augmentation_layers()\n",
    "image_reader.add_preprocessing_layers(normalisation_layers + augmentation_layers)\n",
    "csv_reader = CSVReader(('label',)).initialise(csv_data_param, {'label': (['label'])}, file_list=image_sets_partitioner.all_files)\n",
    "print('One sample from the csv_reader:', np.squeeze(csv_reader(idx=13)[1]['label']))\n",
    "window_sizes = {'image': (100, 100, 1), 'label': (1, 1, 1)}\n",
    "sampler = ResizeSampler(reader=image_reader,\n",
    "                        csv_reader=csv_reader,\n",
    "                        window_sizes=window_sizes,\n",
    "                        num_threads=2,\n",
    "                        smaller_final_batch_mode='drop',\n",
    "                        batch_size=2,\n",
    "                        queue_length=2)\n",
    "sample = next(sampler())\n",
    "print(sample['image'].shape)\n",
    "print(sample['label'].shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2) Create a csv of features and show how it can be returned by the CSV Reader\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accessing: https://github.com/NifTK/NiftyNetModelZoo\n",
      "mr_ct_regression_model_zoo_data: FAIL. \n",
      "No NiftyNet example was found for mr_ct_regression_model_zoo_data.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>subject_id</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>RAN</td>\n",
       "      <td>-2.018905</td>\n",
       "      <td>0.016700</td>\n",
       "      <td>0.753500</td>\n",
       "      <td>0.225668</td>\n",
       "      <td>-0.453137</td>\n",
       "      <td>0.007740</td>\n",
       "      <td>0.405050</td>\n",
       "      <td>-0.038025</td>\n",
       "      <td>-1.144459</td>\n",
       "      <td>-0.437604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HAL</td>\n",
       "      <td>0.750608</td>\n",
       "      <td>1.339236</td>\n",
       "      <td>0.393103</td>\n",
       "      <td>0.403952</td>\n",
       "      <td>0.034992</td>\n",
       "      <td>1.743924</td>\n",
       "      <td>0.222566</td>\n",
       "      <td>0.411477</td>\n",
       "      <td>-0.557221</td>\n",
       "      <td>0.407324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MIL</td>\n",
       "      <td>-0.581589</td>\n",
       "      <td>-0.988355</td>\n",
       "      <td>-1.536794</td>\n",
       "      <td>0.344513</td>\n",
       "      <td>-0.161370</td>\n",
       "      <td>-0.118697</td>\n",
       "      <td>-0.322209</td>\n",
       "      <td>0.063637</td>\n",
       "      <td>-1.028091</td>\n",
       "      <td>-0.392706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHA</td>\n",
       "      <td>2.234084</td>\n",
       "      <td>-0.264239</td>\n",
       "      <td>-0.923163</td>\n",
       "      <td>-0.817617</td>\n",
       "      <td>0.388325</td>\n",
       "      <td>0.560078</td>\n",
       "      <td>-0.477164</td>\n",
       "      <td>1.966860</td>\n",
       "      <td>-0.018510</td>\n",
       "      <td>0.442490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>GRA</td>\n",
       "      <td>0.653114</td>\n",
       "      <td>0.477493</td>\n",
       "      <td>0.650812</td>\n",
       "      <td>1.353049</td>\n",
       "      <td>0.734319</td>\n",
       "      <td>-1.539332</td>\n",
       "      <td>0.653350</td>\n",
       "      <td>-0.752011</td>\n",
       "      <td>-0.634769</td>\n",
       "      <td>-0.231503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>PLA</td>\n",
       "      <td>-0.983351</td>\n",
       "      <td>-1.600570</td>\n",
       "      <td>0.223609</td>\n",
       "      <td>-1.100547</td>\n",
       "      <td>-0.430255</td>\n",
       "      <td>1.315413</td>\n",
       "      <td>-0.702730</td>\n",
       "      <td>-1.838355</td>\n",
       "      <td>-0.810046</td>\n",
       "      <td>2.419473</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NAR</td>\n",
       "      <td>0.810422</td>\n",
       "      <td>-2.304446</td>\n",
       "      <td>-0.079281</td>\n",
       "      <td>0.456476</td>\n",
       "      <td>0.643375</td>\n",
       "      <td>0.268823</td>\n",
       "      <td>0.366755</td>\n",
       "      <td>0.530736</td>\n",
       "      <td>1.345358</td>\n",
       "      <td>0.515989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>WEB</td>\n",
       "      <td>0.710464</td>\n",
       "      <td>-0.665521</td>\n",
       "      <td>-1.130775</td>\n",
       "      <td>-0.437872</td>\n",
       "      <td>-0.359845</td>\n",
       "      <td>-0.128057</td>\n",
       "      <td>-0.472871</td>\n",
       "      <td>-0.060746</td>\n",
       "      <td>1.754929</td>\n",
       "      <td>-1.009953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>PAR</td>\n",
       "      <td>1.380408</td>\n",
       "      <td>-0.825570</td>\n",
       "      <td>-0.630116</td>\n",
       "      <td>-2.312509</td>\n",
       "      <td>-0.335770</td>\n",
       "      <td>-1.176994</td>\n",
       "      <td>-0.455428</td>\n",
       "      <td>-0.757432</td>\n",
       "      <td>-0.171152</td>\n",
       "      <td>0.803127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>HON</td>\n",
       "      <td>-0.312656</td>\n",
       "      <td>-0.707139</td>\n",
       "      <td>0.571808</td>\n",
       "      <td>0.682854</td>\n",
       "      <td>0.907817</td>\n",
       "      <td>0.067288</td>\n",
       "      <td>-0.384104</td>\n",
       "      <td>-1.030702</td>\n",
       "      <td>0.015677</td>\n",
       "      <td>-0.808565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>HAF</td>\n",
       "      <td>2.413476</td>\n",
       "      <td>-0.258832</td>\n",
       "      <td>0.351502</td>\n",
       "      <td>-1.447595</td>\n",
       "      <td>-0.861127</td>\n",
       "      <td>1.074561</td>\n",
       "      <td>-0.550000</td>\n",
       "      <td>1.082476</td>\n",
       "      <td>0.487512</td>\n",
       "      <td>-0.926261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LEW</td>\n",
       "      <td>0.466895</td>\n",
       "      <td>-0.375061</td>\n",
       "      <td>0.657998</td>\n",
       "      <td>1.203485</td>\n",
       "      <td>1.347131</td>\n",
       "      <td>0.552526</td>\n",
       "      <td>-0.705073</td>\n",
       "      <td>1.992426</td>\n",
       "      <td>-0.816416</td>\n",
       "      <td>0.532992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>SOU</td>\n",
       "      <td>-0.009581</td>\n",
       "      <td>-0.436214</td>\n",
       "      <td>-0.600287</td>\n",
       "      <td>2.147111</td>\n",
       "      <td>0.839317</td>\n",
       "      <td>-0.444572</td>\n",
       "      <td>0.197550</td>\n",
       "      <td>0.548611</td>\n",
       "      <td>0.334053</td>\n",
       "      <td>-1.498843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>SPE</td>\n",
       "      <td>0.238420</td>\n",
       "      <td>-0.966039</td>\n",
       "      <td>-0.073183</td>\n",
       "      <td>1.595601</td>\n",
       "      <td>-0.093269</td>\n",
       "      <td>-1.048532</td>\n",
       "      <td>-0.657099</td>\n",
       "      <td>-1.178905</td>\n",
       "      <td>0.795620</td>\n",
       "      <td>-0.974147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>CRI</td>\n",
       "      <td>-0.188791</td>\n",
       "      <td>1.611999</td>\n",
       "      <td>0.423356</td>\n",
       "      <td>-1.644961</td>\n",
       "      <td>-0.961844</td>\n",
       "      <td>-1.446989</td>\n",
       "      <td>0.869527</td>\n",
       "      <td>-1.843371</td>\n",
       "      <td>-2.446698</td>\n",
       "      <td>-1.428567</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   subject_id         0         1         2         3         4         5  \\\n",
       "0         RAN -2.018905  0.016700  0.753500  0.225668 -0.453137  0.007740   \n",
       "1         HAL  0.750608  1.339236  0.393103  0.403952  0.034992  1.743924   \n",
       "2         MIL -0.581589 -0.988355 -1.536794  0.344513 -0.161370 -0.118697   \n",
       "3         CHA  2.234084 -0.264239 -0.923163 -0.817617  0.388325  0.560078   \n",
       "4         GRA  0.653114  0.477493  0.650812  1.353049  0.734319 -1.539332   \n",
       "5         PLA -0.983351 -1.600570  0.223609 -1.100547 -0.430255  1.315413   \n",
       "6         NAR  0.810422 -2.304446 -0.079281  0.456476  0.643375  0.268823   \n",
       "7         WEB  0.710464 -0.665521 -1.130775 -0.437872 -0.359845 -0.128057   \n",
       "8         PAR  1.380408 -0.825570 -0.630116 -2.312509 -0.335770 -1.176994   \n",
       "9         HON -0.312656 -0.707139  0.571808  0.682854  0.907817  0.067288   \n",
       "10        HAF  2.413476 -0.258832  0.351502 -1.447595 -0.861127  1.074561   \n",
       "11        LEW  0.466895 -0.375061  0.657998  1.203485  1.347131  0.552526   \n",
       "12        SOU -0.009581 -0.436214 -0.600287  2.147111  0.839317 -0.444572   \n",
       "13        SPE  0.238420 -0.966039 -0.073183  1.595601 -0.093269 -1.048532   \n",
       "14        CRI -0.188791  1.611999  0.423356 -1.644961 -0.961844 -1.446989   \n",
       "\n",
       "           6         7         8         9  \n",
       "0   0.405050 -0.038025 -1.144459 -0.437604  \n",
       "1   0.222566  0.411477 -0.557221  0.407324  \n",
       "2  -0.322209  0.063637 -1.028091 -0.392706  \n",
       "3  -0.477164  1.966860 -0.018510  0.442490  \n",
       "4   0.653350 -0.752011 -0.634769 -0.231503  \n",
       "5  -0.702730 -1.838355 -0.810046  2.419473  \n",
       "6   0.366755  0.530736  1.345358  0.515989  \n",
       "7  -0.472871 -0.060746  1.754929 -1.009953  \n",
       "8  -0.455428 -0.757432 -0.171152  0.803127  \n",
       "9  -0.384104 -1.030702  0.015677 -0.808565  \n",
       "10 -0.550000  1.082476  0.487512 -0.926261  \n",
       "11 -0.705073  1.992426 -0.816416  0.532992  \n",
       "12  0.197550  0.548611  0.334053 -1.498843  \n",
       "13 -0.657099 -1.178905  0.795620 -0.974147  \n",
       "14  0.869527 -1.843371 -2.446698 -1.428567  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from niftynet.utilities.download import download\n",
    "download('mr_ct_regression_model_zoo_data')\n",
    "labels_location = 'ct.csv'\n",
    "files = [file.replace('.nii.gz', '') for file in os.listdir('/home/tom/niftynet/data/mr_ct_regression/CT_zero_mean') if file.endswith('.nii.gz')]\n",
    "\n",
    "pd.DataFrame(data=[tuple([file] + list(np.random.randn(10))) for file in files]).to_csv('features.csv', index=None, header=['subject_id'] + [str(x) for x in range(10)])\n",
    "pd.read_csv('features.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1mINFO:niftynet:\u001b[0m \n",
      "\n",
      "Number of subjects 15, input section names: ['subject_id', 'CT']\n",
      "-- using all subjects (without data partitioning).\n",
      "\n",
      "\u001b[1mINFO:niftynet:\u001b[0m Image reader: loading 15 subjects from sections ['CT'] as input [image]\n",
      "\u001b[1mWARNING:niftynet:\u001b[0m This method will read your entire csv into memory\n",
      "One sample from the csv_reader: [ 0.23841972 -0.96603888 -0.07318273  1.59560139 -0.09326917 -1.04853203\n",
      " -0.65709902 -1.17890471  0.7956195  -0.97414747]\n",
      "\u001b[1mINFO:niftynet:\u001b[0m reading size of preprocessed images\n",
      "\u001b[1mWARNING:niftynet:\u001b[0m sampler queue_length should be larger than batch_size, defaulting to batch_size * 5.0 (10).\n",
      "(1, 100, 100, 1, 1, 1)\n",
      "(1, 10, 1, 1, 1, 1)\n",
      "dict_keys(['image_location', 'image', 'features', 'features_location'])\n"
     ]
    }
   ],
   "source": [
    "task_param = TaskParam(['image'])\n",
    "image_data_param = {'CT': {'path_to_search': '~/niftynet/data/mr_ct_regression/CT_zero_mean', 'filename_contains': 'nii'}}\n",
    "csv_data_param = {'features': {'csv_data_file': 'features.csv', 'to_ohe': False}}\n",
    "grouping_param = {'image': (['CT'])}\n",
    "image_sets_partitioner = ImageSetsPartitioner().initialise(image_data_param)\n",
    "image_reader = ImageReader().initialise(image_data_param, grouping_param, file_list=image_sets_partitioner.all_files)\n",
    "preprocessing = Preprocessing(net_param, action_param, task_param)\n",
    "normalisation_layers = preprocessing.prepare_normalisation_layers()\n",
    "augmentation_layers = preprocessing.prepare_augmentation_layers()\n",
    "image_reader.add_preprocessing_layers(normalisation_layers + augmentation_layers)\n",
    "csv_reader = CSVReader(('features',)).initialise(csv_data_param, {'features': ['features']}, file_list=image_sets_partitioner.all_files)\n",
    "print('One sample from the csv_reader:', np.squeeze(csv_reader(idx=13)[1]['features']))\n",
    "window_sizes = {'image': (100, 100, 1), 'features': (1, 1, 1)}\n",
    "sampler = ResizeSampler(reader=image_reader,\n",
    "                        csv_reader=csv_reader,\n",
    "                        window_sizes=window_sizes,\n",
    "                        num_threads=2,\n",
    "                        smaller_final_batch_mode='drop',\n",
    "                        batch_size=2,\n",
    "                        queue_length=2)\n",
    "sample = next(sampler())\n",
    "print(sample['image'].shape)\n",
    "print(sample['features'].shape)\n",
    "print(sample.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing the CSV Reader on labels AND Features\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1mINFO:niftynet:\u001b[0m \n",
      "\n",
      "Number of subjects 15, input section names: ['subject_id', 'CT']\n",
      "-- using all subjects (without data partitioning).\n",
      "\n",
      "\u001b[1mINFO:niftynet:\u001b[0m Image reader: loading 15 subjects from sections ['CT'] as input [image]\n",
      "\u001b[1mWARNING:niftynet:\u001b[0m This method will read your entire csv into memory\n",
      "\u001b[1mWARNING:niftynet:\u001b[0m This method will read your entire csv into memory\n",
      "One sample from the csv_reader: [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
      "\u001b[1mINFO:niftynet:\u001b[0m reading size of preprocessed images\n",
      "\u001b[1mWARNING:niftynet:\u001b[0m sampler queue_length should be larger than batch_size, defaulting to batch_size * 5.0 (10).\n",
      "(1, 100, 100, 1, 1, 1)\n",
      "(1, 15, 1, 1, 1, 1)\n",
      "(1, 10, 1, 1, 1, 1)\n"
     ]
    }
   ],
   "source": [
    "# Make sure we accept 'Label', 'label', 'LABEL'\n",
    "task_param = TaskParam(['image'])\n",
    "image_data_param = {'CT': {'path_to_search': '~/niftynet/data/mr_ct_regression/CT_zero_mean', 'filename_contains': 'nii'}}\n",
    "csv_data_param = {'label': {'csv_data_file': 'label.csv', 'to_ohe': True},\n",
    "                  'features': {'csv_data_file': 'features.csv', 'to_ohe': False}}\n",
    "grouping_param = {'image': (['CT'])}\n",
    "\n",
    "image_sets_partitioner = ImageSetsPartitioner().initialise(image_data_param)\n",
    "image_reader = ImageReader().initialise(image_data_param, grouping_param, file_list=image_sets_partitioner.all_files)\n",
    "preprocessing = Preprocessing(net_param, action_param, task_param)\n",
    "normalisation_layers = preprocessing.prepare_normalisation_layers()\n",
    "augmentation_layers = preprocessing.prepare_augmentation_layers()\n",
    "image_reader.add_preprocessing_layers(normalisation_layers + augmentation_layers)\n",
    "\n",
    "csv_reader = CSVReader(('label', 'features')).initialise(csv_data_param,\n",
    "                                                         {'label': (['label']), 'features': (['features'])},\n",
    "                                                         file_list=image_sets_partitioner.all_files)\n",
    "\n",
    "\n",
    "print('One sample from the csv_reader:', np.squeeze(csv_reader(idx=13)[1]['label']))\n",
    "window_sizes = {'image': (100, 100, 1), 'label': (1, 1, 1)}\n",
    "sampler = ResizeSampler(reader=image_reader,\n",
    "                        csv_reader=csv_reader,\n",
    "                        window_sizes=window_sizes,\n",
    "                        num_threads=2,\n",
    "                        smaller_final_batch_mode='drop',\n",
    "                        batch_size=2,\n",
    "                        queue_length=2)\n",
    "sample = next(sampler())\n",
    "print(sample['image'].shape)\n",
    "print(sample['label'].shape)\n",
    "print(sample['features'].shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "modalities = ['t1ce.', 't1.', 'flair.', 't2.']\n",
    "def get_modality(string):\n",
    "    return modalities[[True if mod in string else False for mod in modalities].index(True)][:-1]\n",
    "    \n",
    "files = [(file.replace('.nii.gz', ''), get_modality(file)) \\\n",
    "         for file in os.listdir('/home/tom/data/BRATS_18_SPLITS/train') if 'seg' not in file]\n",
    "pd.DataFrame(data=files, columns=['subject_id', 'label']).to_csv('/home/tom/phd/NiftyNet-Generator-PR/NiftyNet/modality_labels.csv', index=None)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
